package com.qqtech.qquan.circle.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.enums.CircleConst;
import com.qqtech.qquan.circle.enums.CircleUpgradeApplyStatusDictEnum;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.model.CircleUpgradeApply;
import com.qqtech.qquan.circle.model.CircleUpgradeApplyQuery;
import com.qqtech.qquan.circle.model.CircleUpgradeApplyVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleSysDictService;
import com.qqtech.qquan.circle.service.CircleUpgradeApplyService;
import com.qqtech.qquan.community.model.CommunityBaseQuery;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.sys.model.SysAreaVo;
import com.qqtech.qquan.sys.service.SysAreaService;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;

@Controller
@RequestMapping(value = "/circle/upgrade/apply")
public class CircleUpgradeApplyController extends BaseAdminController {
	@Resource
	private CircleUpgradeApplyService circleUpgradeApplyService;

	@Resource
	private CircleBaseService circleBaseService;
	@Resource
	private UserBaseService userBaseService;
	@Resource
	private CircleSysDictService circleSysDictService;
	@Resource
	private CommunityBaseService communityBaseService;
	@Resource
	private SysAreaService  sysAreaService;
	
	@Override
	protected BaseService<CircleUpgradeApply> getBaseService() {
		return circleUpgradeApplyService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/circle/circleUpgradeApply-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(CircleUpgradeApplyQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		query.setStatus(CircleUpgradeApplyStatusDictEnum.TODO.getCode());
		Pageable pageKit = new PageKit(query.getPage(),query.getSize(),  new PageOrder(Direction.DESC, "id"));
		Page<CircleUpgradeApplyVo> page = circleUpgradeApplyService.queryPageList(query, pageKit);
		UserBaseVo userBaseVo = null;
		CircleBaseVo circleVo = null;
		String lastToExtCommunityIds = "";//已扩展社区ids
		String toExtCommunityIds = "";//本次扩展社区ids
		for (CircleUpgradeApplyVo applyVo : page) {
			userBaseVo = userBaseService.getById(applyVo.getApplyId());//申请人
			circleVo = circleBaseService.getById(applyVo.getCircleId());//圈子
			if (null != userBaseVo) {
				applyVo.setApplyName(userBaseVo.getNick());
			}
			if(null != circleVo){
				applyVo.setCircleName(circleVo.getName());
			}
			
			if(StringUtil.isNotBlank(lastToExtCommunityIds)){
				String[] strCommunityIds = lastToExtCommunityIds.split(";");
				List<Integer> CommunityIds = getIntegerCommunityIds(strCommunityIds);
				if(!CommunityIds.isEmpty()){
					CommunityBaseQuery communityQ = new CommunityBaseQuery();
					communityQ.setIncludeIds(CommunityIds);
					List<CommunityBaseVo> communityList = communityBaseService.queryList(communityQ);
					applyVo.setLastToExtCommunityNames(getCommunityNames(communityList));
				}
			}else{
				applyVo.setLastToExtCommunityNames("未扩展");
			}
			applyVo.setFromZoneLevelName(circleSysDictService.dictValue(CircleConst.DICTYPE_CIRCLE_ZONE_LEVEL, String.valueOf(applyVo.getFromZoneLevel())));
			applyVo.setToZoneLevelName(circleSysDictService.dictValue(CircleConst.DICTYPE_CIRCLE_ZONE_LEVEL, String.valueOf(applyVo.getToZoneLevel())));
			lastToExtCommunityIds = circleVo.getExtCommunityIds();//已扩展社区ids
			if(StringUtil.isNotBlank(lastToExtCommunityIds)){
				String[] strCommunityIds = lastToExtCommunityIds.split(";");
				List<Integer> CommunityIds = getIntegerCommunityIds(strCommunityIds);
				if(!CommunityIds.isEmpty()){
					CommunityBaseQuery communityQ = new CommunityBaseQuery();
					communityQ.setIncludeIds(CommunityIds);
					List<CommunityBaseVo> communityList = communityBaseService.queryList(communityQ);
					applyVo.setLastToExtCommunityNames(getCommunityNames(communityList));
				}
			}else{
				applyVo.setLastToExtCommunityNames("未扩展");
			}
			
			toExtCommunityIds = applyVo.getToExtCommunityIds();
			if(StringUtil.isNotBlank(toExtCommunityIds)){
				String[] strCommunityIds = toExtCommunityIds.split(";");
				List<Integer> CommunityIds = getIntegerCommunityIds(strCommunityIds);
				if(!CommunityIds.isEmpty()){
					CommunityBaseQuery communityQ = new CommunityBaseQuery();
					communityQ.setIncludeIds(CommunityIds);
					List<CommunityBaseVo> communityList = communityBaseService.queryList(communityQ);
					applyVo.setToExtCommunityNames(getCommunityNames(communityList));
				}
			}
			if(applyVo.getToCityId()!=null){
				SysAreaVo area = sysAreaService.getById(applyVo.getToCityId());
				if(area!=null){
					applyVo.setToCityName(area.getName());
				}
			}else{
				applyVo.setToCityName("未扩展");
			}
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	@RequestMapping(value = "/detail")
	public String detail(Integer id, Model model) {
		CircleUpgradeApplyVo circleUpgradeApplyVo = circleUpgradeApplyService.getById(id);
		UserBaseVo userBaseVo = userBaseService.getById(circleUpgradeApplyVo.getApplyId());
		if(userBaseVo!=null){
			circleUpgradeApplyVo.setApplyName(userBaseVo.getNick());
		}
		CircleBaseVo circleVo = circleBaseService.getById(circleUpgradeApplyVo.getCircleId());
		if(circleVo!=null){
			circleUpgradeApplyVo.setCircleName(circleVo.getName());
			CommunityBaseVo communityVo = communityBaseService.getById(circleVo.getCommunityId());
			if(communityVo!=null){
				circleUpgradeApplyVo.setCommunityName(communityVo.getName());
			}
		}
		circleUpgradeApplyVo.setFromZoneLevelName(circleSysDictService.dictValue(CircleConst.DICTYPE_CIRCLE_ZONE_LEVEL, String.valueOf(circleUpgradeApplyVo.getFromZoneLevel())));
		circleUpgradeApplyVo.setToZoneLevelName(circleSysDictService.dictValue(CircleConst.DICTYPE_CIRCLE_ZONE_LEVEL, String.valueOf(circleUpgradeApplyVo.getToZoneLevel())));
		if(circleUpgradeApplyVo.getToCityId()!=null){
			SysAreaVo area = sysAreaService.getById(circleUpgradeApplyVo.getToCityId());
			if(area!=null){
				circleUpgradeApplyVo.setToCityName(area.getName());
			}
		}
		String lastToExtCommunityIds = circleVo.getExtCommunityIds();//已扩展社区ids
		if(StringUtil.isNotBlank(lastToExtCommunityIds)){
			String[] strCommunityIds = lastToExtCommunityIds.split(";");
			List<Integer> CommunityIds = getIntegerCommunityIds(strCommunityIds);
			if(!CommunityIds.isEmpty()){
				CommunityBaseQuery communityQ = new CommunityBaseQuery();
				communityQ.setIncludeIds(CommunityIds);
				List<CommunityBaseVo> communityList = communityBaseService.queryList(communityQ);
				circleUpgradeApplyVo.setLastToExtCommunityNames(getCommunityNames(communityList));
			}
		}else{
			circleUpgradeApplyVo.setLastToExtCommunityNames("未扩展");
		}
		String toExtCommunityIds = circleUpgradeApplyVo.getToExtCommunityIds();//本次扩展社区ids
		if(StringUtil.isNotBlank(toExtCommunityIds)){
			String[] strCommunityIds = toExtCommunityIds.split(";");
			List<Integer> CommunityIds = getIntegerCommunityIds(strCommunityIds);
			if(!CommunityIds.isEmpty()){
				CommunityBaseQuery communityQ = new CommunityBaseQuery();
				communityQ.setIncludeIds(CommunityIds);
				List<CommunityBaseVo> communityList = communityBaseService.queryList(communityQ);
				circleUpgradeApplyVo.setToExtCommunityNames(getCommunityNames(communityList));
			}
		}
		if(circleUpgradeApplyVo.getToCityId()!=null){
			SysAreaVo area = sysAreaService.getById(circleUpgradeApplyVo.getToCityId());
			if(area!=null){
				circleUpgradeApplyVo.setToCityName(area.getName());
			}
		}else{
			circleUpgradeApplyVo.setToCityName("未扩展");
		}
		model.addAttribute("circleUpgradeApplyVo", circleUpgradeApplyVo);
		return "/circle/circleUpgradeApply-detail";
	}

	/**
	 * 审核通过
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/auditPass")
	@ResponseBody
	public Result auditPass(Integer id) {
		Result ret = new Result();
		circleUpgradeApplyService.auditPass(id);
		return ret;
	}
	
	/**
	 * 审核不通过
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/auditNoPass")
	@ResponseBody
	public Result auditNoPass(Integer id) {
		Result ret = new Result();
		circleUpgradeApplyService.auditNoPass(id, "");
		return ret;
	}
	
	/**
	 * 返回社区字符串【以、号隔开】
	 * @param communityList
	 * @return
	 */
	private String getCommunityNames(List<CommunityBaseVo> communityList){
		String names = "";
		if(communityList!=null && communityList.size() >0){
			for(CommunityBaseVo community : communityList){
				names += community.getName() +"、";
			}
		}
		if(StringUtil.isNotBlank(names)){
			names = names.substring(0, names.length()-1);
		}
		return names;
	}
	
	/**
	 * List<String>转换为List<Integer>
	 * @param strCommunityIds
	 * @return
	 */
	private List<Integer> getIntegerCommunityIds(String[] strCommunityIds){
		List<Integer> communityIds = Lists.newArrayList();
		if(strCommunityIds!=null && strCommunityIds.length >0){
			for(String strCommunityId : strCommunityIds){
				communityIds.add(Integer.parseInt(strCommunityId));
			}
		}
		
		return communityIds;
	}
}
